package com.ruoyi.epidemic.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.epidemic.service.IYqInfoCollectService;
import com.ruoyi.epidemic.service.IsmsService;
import com.ruoyi.epidemic.util.getWeCpService;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpMessageService;
import me.chanjar.weixin.cp.api.impl.WxCpMessageServiceImpl;
import me.chanjar.weixin.cp.api.impl.WxCpOAuth2ServiceImpl;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.bean.message.*;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class IsmsServiceImpl implements IsmsService {

    protected Logger logger = LoggerFactory.getLogger(IsmsServiceImpl.class);
    @Autowired
    private getWeCpService wecpservice;

    @Value("${wechat.cp.appConfigs.agentId}")
    private Integer agentId;

    @Override
    public void send(String toUser, String toParty, String toTag, String content) throws WxErrorException{
        WxCpMessage message=new WxCpMessage();
        message.setToUser(toUser);
        message.setToParty(toParty);
        message.setToTag(toTag);
        message.setMsgType("text");
        message.setAgentId(agentId);
        message.setContent(content);
        WxCpMessageServiceImpl wxCpMessageService = new WxCpMessageServiceImpl(wecpservice.getWxCpServiceImpl());
        try {
            WxCpMessageSendResult result=wxCpMessageService.send(message);
            logger.info("result:"+result);
        }catch(WxErrorException e){
            logger.error(e.toString());
        }
    }

    @Override
    public void send(String toUser, String toParty, String toTag, String content,String safe,Boolean enableIdTrans,Boolean enableDuplicateCheck,Integer duplicateCheckInterval) throws WxErrorException {
        WxCpMessage message=new WxCpMessage();
        message.setToUser(toUser);
        message.setToParty(toParty);
        message.setToTag(toTag);
        message.setMsgType("text");
        message.setAgentId(agentId);
        message.setContent(content);
        message.setSafe(safe);
        message.setEnableIdTrans(enableIdTrans);
        message.setEnableDuplicateCheck(enableDuplicateCheck);
        message.setDuplicateCheckInterval(duplicateCheckInterval);
        WxCpMessageServiceImpl wxCpMessageService = new WxCpMessageServiceImpl(wecpservice.getWxCpServiceImpl());
        try {
        WxCpMessageSendResult result=wxCpMessageService.send(message);
            logger.info("result:"+result);
        }catch(WxErrorException e){
            logger.error(e.toString());
        }
    }
}
